(function () {
    $.fn.extend({
        add() {
            let u_Id = JSON.parse(localStorage.getItem("avc123") || "{}").u_id;
            var objects = { u_Id: u_Id }
            // $.cookie.json = true;
            // var localData = $.cookie("cart");
            $.ajax({
                url: "/account/getCartById",
                type: "post",
                data: objects
            }).then(function (response) {
                $(".reduce_g").on("click", function () {
                   
                    var c_Id=$(this).data("info")
                    var num = $(this).siblings(".input-goods").find("input").val()
                    $(this).siblings(".input-goods").find("input").val(num - 1)
                    var price = $(this).parent(".contype1_cont3").siblings(".contype1_cont2").text().split("￥")[1]
                    $(this).parent(".contype1_cont3").siblings(".contype1_cont5").find("span").text("￥" + (num - 1) * price)
                    if (num <= 0) {
                        $(this).siblings(".input-goods").find("input").val(0)
                    }
                    if ((num - 1) * price <= 0) {
                        $(this).parent(".contype1_cont3").siblings(".contype1_cont5").find("span").text(0)
                    }
                    var tol=0;
                    $(".box").find(".contype1_cont5 span").each(function (index, item) {
                     tol+=Number($(item).text().split("￥")[1])
                       return(tol)
                    })
                    console.log(tol);
                    $("#j-totaltopay6").text("￥"+tol)
                    var c_number = $(this).siblings(".input-goods").find("input").val()
                    response.data.forEach(item => {
                        if(item.c_id==c_Id){
                            var list = Object.assign({c_number:c_number},{u_Id:item.u_id},{c_name:item.c_name})
                            console.log(list);
                            $.ajax({
                                url: "/account/changecart",
                                type: "POST",
                                data: list
                            }).then(function (response) {
                                console.log(response);
                            })
                        }
                      
                    })

                })
                $(".plus_g").on("click", function () {

                    var c_Id=$(this).data("info")
                    var num = $(this).siblings(".input-goods").find("input").val()
                    var price = $(this).parent(".contype1_cont3").siblings(".contype1_cont2").text().split("￥")[1]
                    $(this).siblings(".input-goods").find("input").val(Number(num) + 1)
                    var c_number = $(this).siblings(".input-goods").find("input").val()
                    $(this).parent(".contype1_cont3").siblings(".contype1_cont5").find("span").text("￥" + (Number(num) + 1) * price)
                    var tol=0;
                    $(".box").find(".contype1_cont5 span").each(function (index, item) {
                     tol+=Number($(item).text().split("￥")[1])
                       return(tol)
                    })
                    console.log(tol);
                    $("#j-totaltopay6").text("￥"+tol)
                    response.data.forEach(item => {
                        item.c_total+=item.c_total
                        if(item.c_id==c_Id){
                            var list = Object.assign({c_number:c_number},{u_Id:item.u_id},{c_name:item.c_name})
                            $.ajax({
                                url: "/account/changecart",
                                type: "POST",
                                data: list
                            }).then(function (response) {
                                console.log(response);
                            })
                        }
                      
                    })
                })
                $(".fath_div_1 .operation_a").on("click", function () {
                   var c_Id=$(this).data("info")
                    alert("确认删除商品吗")
                    $.ajax({
                        url:"/account/deleteCart",
                        type:"post",
                        data:{c_id:c_Id}
                    }).then(function (response) {
                        console.log(response);
                    })
                })
                $(".topay").on("click", function () {
                    if ($(".content_type1 input").prop("checked") == true) {
                        alert("购买成功")
                    } else {
                        alert("请选择要购买商品")
                    }

                })
            })
        },
        tabe() {
            let u_Id = JSON.parse(localStorage.getItem("avc123") || "{}").u_id;
            var objects = { u_Id: u_Id }
            $.ajax({
                url: "/account/getCartById",
                type: "post",
                data: objects
            }).then(function (response) {
                var c_id=response.data[0].c_id;
                var strhtml = ``;
                response.data.forEach(item => {
                    strhtml += ` 
                    <div class="box">
                    <div class="ct1 contype1_cont1 ct_zp">
                    <div>
                        <input type="checkbox" class="fl">
                        <a href="" class="img_a"><img src="${item.c_pic}" alt=""></a>
                        <div class="contype1_cont1_cent fl"><a href="" class="content1_a">${item.c_name}</a></div>
                    </div>
                </div>
                <div class="ct1 contype1_cont2 ct_zp">￥${item.c_price}</div>
                <div class="ct1 contype1_cont3 ct_zp">
                    <div class="reduce_g fl oper_goods" data-info=${item.c_id}>-</div>
                    <div class="input-goods fl"> <input type="text" value="${item.c_number}"></div>
                    <div class="plus_g fl oper_goods" data-info=${item.c_id}>+</div>
                </div>
                <div class="ct1 contype1_cont4 ct_zp">2.01</div>
                <div class="ct1 contype1_cont5 ct_zp">
                    <span>￥${item.c_price * item.c_number}</span>
                </div>
                <div class="ct1 contype1_cont6 ct_zp">
                    <a href="" class="operation_a">
                        收藏
                    </a>
                    <div class="fath_div_1"> <a href="" class="operation_a" data-info=${item.c_id}>
                            删除
                        </a></div>
                </div>
                </div>`
               
             
                })
                $(".content_type").html(strhtml)
                $(".cart_content").add()
            })
        },

    })
    $(function () {
        $(document).tabe();
        // $(document).add()
    })
})()